package net.getbang.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import net.getbang.admin.entity.SysUserRole;
import net.getbang.admin.mapper.SysUserRoleMapper;
import net.getbang.admin.service.SysUserRoleService;

@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	@Override
	public List<Long> queryRoleIdList(Long userId) {
		// TODO Auto-generated method stub
		return sysUserRoleMapper.queryRoleIdList(userId);
	}

	@Override
	public void saveOrUpdate(Long userId, List<Long> roleIdList) {
		// TODO Auto-generated method stub
		// 先删除用户与角色关系
		//this.delete(userId);
		Map<String, Object> map = new HashMap<>();
		map.put("user_id", userId);
		sysUserRoleMapper.deleteByMap(map);

		if (roleIdList.isEmpty()) {
			return;
		}

		// 保存用户与角色关系
		map.put("userId", userId);
		map.put("roleIdList", roleIdList);
		
		sysUserRoleMapper.save(map);

	}

}
